/*
 * @Author: your name
 * @Date: 2021-02-27 21:30:38
 * @LastEditTime: 2021-04-09 13:42:51
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue-vben-admin-2.0d:\project\tsx\demo-tsx\src\router\permission.ts
 */

import { Router } from 'vue-router';
// import UserStore from 'store/modules/user';
import WebStorage from 'utils/cache/index';
import { Permission } from '@/config/enum';
// import { toRaw } from 'vue';

const whitePathList: Permission[] = [Permission.BASE_LOGIN];

export const routerHooks = (router: Router) => {
  router.beforeEach((to, _form, next) => {
    const token = WebStorage.get('token');
    /** 在白名单内，不需要鉴权 直接进入 */
    if (whitePathList.includes(to.name as Permission) && !token) {
      next();
      return true;
    }

    if (!token) {
      next(Permission.BASE_LOGIN);
      return true;
    }

    if (to.name === Permission.BASE_LOGIN) {
      next(Permission.BASE_LOGIN);
      return true;
    } else {
      next();
      return true;
    }
  });
};
